package com.tushu.service;

import com.tushu.Login;
import com.tushu.dao.BookDao;
import com.tushu.model.Book;

import java.util.List;
import java.util.Scanner;

public class ReturnService {
    private static BookDao bookDao = new BookDao();

    public static void returnBook(int studentId) {
        System.out.println("----我要还书----");
        System.out.println("当前借阅记录：");
        List<Book> lendBookList = bookDao.getLendBookList(studentId);
        System.out.println("id" + "\t\t" + "isbn" + "\t\t" + "书名" + "\t\t" + "作者" + "\t\t" + "出版社");
        for (Book book : lendBookList) {
            System.out.println(book.getId() + "\t\t" + book.getIsbn() + "\t\t" + book.getBookName() + "\t\t" + book.getAuthor() + "\t\t" + book.getPublisher());
        }
        System.out.println("-------1.还书   2.返回------");
        System.out.print("请选择：");
        Scanner s5 = new Scanner(System.in);
        int i = s5.nextInt();
        if (i == 1) {
        //    还书逻辑
            System.out.println("请输入要归还图书的ISBN：");
            Scanner s6 = new Scanner(System.in);
            String isbn = s6.nextLine();
            int bookId = 0;
            for (Book book : lendBookList) {
                if (book.getIsbn().equals(isbn)) {
                    bookId = book.getId();
                }
            }
            if (bookId == 0) {
                System.out.println("输入错误！请重新输入！");
                returnBook(studentId);
            }
            int record = bookDao.deleteLendRecord(studentId, bookId);
            if (record > 0) {
            //    馆藏量+1
                Book book = bookDao.getBookByIsbn(isbn);
                int i1 = bookDao.updateBook1(book.getNum() + 1, book.getId());
                if (i1 > 0) {
                    System.out.println("归还图书成功！");
                    Login.StudentMenu();
                } else {
                    Login.StudentMenu();
                }
            }
        } else if (i == 2) {
            Login.StudentMenu();
        } else {
            System.out.println("输入错误！");
            returnBook(studentId);
        }
    }


}
